﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrmDocman.DataCore;

/// <summary>
/// Summary description for HinhAnhSanPhamHelper
/// </summary>
public class HinhAnhSanPhamHelper : TWSData
{
    public HinhAnhSanPhamHelper()
    {
        m_dao = DataBridge.GetInstance();
    }

    public HinhAnhSanPhamDataSet LoadHinhAnhSanPhamTheoSanPham(int sanpham)
    {

        HinhAnhSanPhamDataSet lspDs = new HinhAnhSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.HinhAnhSanPham);
        sql += " where SanPham = "+sanpham;

        m_dao.FillDataTable(lspDs.HinhAnhSanPham, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public HinhAnhSanPhamDataSet LoadHinhAnhSanPham(int HinhAnhSanPhamID)
    {

        HinhAnhSanPhamDataSet lspDs = new HinhAnhSanPhamDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.HinhAnhSanPham);
        sql += " where ID = " + HinhAnhSanPhamID;

        m_dao.FillDataTable(lspDs.HinhAnhSanPham, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public bool SaveHinhAnhSanPham(HinhAnhSanPhamDataSet lspDs)
    {
        m_dao.UpdateDataTable(lspDs.HinhAnhSanPham);
        m_dao.Connection.CloseConnection();
        return true;

    }

    public HinhAnhSanPhamDataSet LoadHinhAnhSanPham(int page, int pageSize, string sortColumn, string sortOrder)
    {

        HinhAnhSanPhamDataSet spDs = new HinhAnhSanPhamDataSet();
        string sql = "DECLARE @PageNum AS INT; ";
        sql += "DECLARE @PageSize AS INT; ";
        sql += "SET @PageNum = " + page + "; ";
        sql += "SET @PageSize = " + pageSize + "; ";
        sql += "WITH OrdersRN AS ( ";
        sql += "SELECT ROW_NUMBER() OVER(ORDER BY " + sortColumn + " " + sortOrder + ") AS RowNum, * ";
        sql += "FROM dbo.HinhAnhSanPham  ) ";
        sql += "select * from OrdersRN ";
        sql += " WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize ";
        sql += " order by " + sortColumn + " " + sortOrder;

        m_dao.FillDataTable(spDs.HinhAnhSanPham, sql);
        m_dao.Connection.CloseConnection();
        return spDs;
    }
}